
var express = require('express')

var DB = require('../../modules/db')

var fs = require('fs')

var mutiparty = require('multiparty')

var md5 = require('md5-node')

var router = express.Router()  /*可使用express.Router类创建模块化,可挂载的路由句柄*/

router.get('/',function (req, res) {
    //获取get传值id
    var name = req.query.name
    var data;
    if (name) {
        const reg=new RegExp(name,'i');//不区分大小写
        data = {'userAccount':{$regex:reg}}
    }else {
        data = {}
    }
    DB.find('users',data,function (err, data) {
        if (err) {
            console.log('查询失败');
        }else {
            res.render('admin/user/index',{
                list: data,
                searchName: name
            })
        }
    })
})

router.post('/search', function (req, res) {

    var form = new mutiparty.Form()
    form.parse(req, function (err, fields, files) {
        console.log(fields)
        //将上传数据保存到数据库
        var searchName = fields.name[0]
        res.redirect('/admin/user?name='+searchName)
    })
})

router.get('/add',function (req, res) {
    res.render('admin/user/add')
})

router.post('/doAdd',function (req, res) {
    var form = new mutiparty.Form()
    form.uploadDir = 'upload'
    form.parse(req, function (err, fields, files) {

        console.log(fields)

        console.log(files)

        var userName = fields.userName[0]
        var description = fields.description[0]

        var head = files.head[0].path

        DB.insert('users',{
            'userAccount':userName,
            description,
            head
        },function (err, data) {
            if (!err) {
                res.redirect('/admin/user')
            }
        })

    })
})

router.get('/edit',function (req, res) {

    //获取编辑id
    var id = req.query.id
    
    DB.find('users',{'_id': new DB.ObjectID(id)}, function (err, data) {
        if (err) {
            console.log('查询失败');
        }else {
            console.log('data is', data)
            res.render('admin/user/edit',{list:data[0]})
        }
    })

})

router.post('/doEdit',function (req, res) {

    var form = new mutiparty.Form()
    form.uploadDir = 'upload'
    form.parse(req, function (err, fields, files) {

        console.log(fields)

        console.log(files)

        var id = fields._id[0]
        var userName = fields.userName[0]
        var password = fields.password[0]
        var description = fields.description[0]

        var pic = files.head[0].path

        var setData
        //判断图片是否为空
        var originalFileName = files.head[0].originalFilename
        if (originalFileName) {
            if (password) {
                setData = {
                    'userAccount':userName,
                    'password': md5(password),
                    description,
                    head
                }
            }else {
                setData = {
                    'userAccount':userName,
                    description,
                    head
                }
            }

            DB.find('users',{
                '_id': new DB.ObjectID(id)
            }, function (err, data) {
                if (!err) {
                    console.log('data is', data)
                    fs.unlink(data[0].head,function () {

                    })
                }
            })
        }else {
            if (password) {
                setData = {
                    'userAccount':userName,
                    'password': md5(password),
                    description
                }
            }else {
                setData = {
                    'userAccount':userName,
                    description
                }
            }
            //删除生成的临时文件
            fs.unlink(pic,function () {

            })
        }

        DB.update('users',{'_id':DB.ObjectID(id)},setData,function (err, data) {
            if (!err) {
                res.redirect('/admin/user')
            }
        })

    })

})

router.get('/delete',function (req, res) {
    //获取编辑id
    var id = req.query.id

    //删除生成的临时文件
    DB.find('users',{
        '_id': new DB.ObjectID(id)
    }, function (err, data) {

        if (!err) {

            fs.unlink(data[0].head,function () {

            })

            DB.deleteOne('users',{'_id': new DB.ObjectID(id)}, function (err, data) {
                if (err) {
                    console.log('删除失败');
                }else {
                    res.redirect('/admin/user')
                }
            })
        }
    })
})

module.exports = router